;
(function (call) {
    var main = {
        dzzhStation: 'A2101,A2102,A2103,A2104,A2105,A2106,A2107,A2108,A2109,A2110,A2111,A2112,A2113,A2114,A2115,A2116,A2117,A2118,A2119,A2120,A2121,A2122,A2123,A2124,A2125,A2126,A2127,A2128,A2129,A2130,A2131,A2132,A2133,A2161,A2162,A2163,A2164,A2165,A2166,A2168,A2169',
        lon: [],
        lat: [],
        areaData: [],
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $("#publishTime").val(new Date().format('dd日'));
            this.queryStationByIds();
            this.getDzzhybDataDetail();
        },
        // 查询各站点经纬度
        queryStationByIds: function () {
            var that = this;
            $.ajax({
                url: api.internal.dzzhYb.queryStationByIds,
                data: { ids: that.dzzhStation },
                dataType: 'JSON',
                async: false,
                type: 'GET',
                success: function (res) {
                    if (!res.isSuccess) return;
                    var data = res.data;
                    for (var key in data) {
                        if (data[key]) {
                            that.lon.push(data[key].lon);
                            that.lat.push(data[key].lat);
                        }
                    }
                    that.areaData = data;
                }
            });
        },
        getParams: function () {
            var params = {
                fcstTime: new Date().format('yyyyMMdd080000'),
                element: 'TP',
                startPeriod: 7,
                endPeriod: 12,
                interval: 1,
                stationType: 0,
                lat: this.lat.join(','),
                lon: this.lon.join(',')
            }
            return params;
        },
        // 初始化地质灾害表格
        initDzzhStation: function () {
            var that = this;
            var $body = $(".dzzh_table tbody").empty();
            var param = {
                bjdateTime: new Date().format('yyyyMMdd080000'),
                element: 'RAIN_24H',
                stationId: that.dzzhStation
            }
			var param2 = {
                bjdateTime: new Date().format('yyyyMMdd140000'),
                element: 'RAIN_6H',
                stationId: that.dzzhStation
            }
			var param4 = {
                bjdateTime: new Date().format('yyyyMMdd150000'),
                element: 'RAIN_1H',
                stationId: that.dzzhStation
            }
            var param3 = that.getParams();
            param3.startPeriod = 12;
            param3.endPeriod = 36;
            param3.interval = 24;
            loading.add('.cafe-container');
            $.when(
                $.getJSON(api.external.getSkValFromHour, param),
				$.getJSON(api.external.getSkValFromHour, param2),
                $.getJSON(api.external.getGdybDataByParamData, that.getParams()),
                $.getJSON(api.external.getGdybDataByParamData, param3),
				$.getJSON(api.external.getSkValFromHour, param4),
            ).then(function (res, res2, res3, res4, res5) {
                loading.remove();
                var data = res[0].resultData;
                var data2 = res2[0].resultData;
                var data3 = res3[0].resultData;
				var data4 = res4[0].resultData;
				var data5 = res5[0].resultData;
                var tableData = [];
				that.dzzhStation.split(',').forEach(function(stationId){
					var station = that.areaData[stationId];
					var obj = {};
                    obj.stationName = stationId;
                    if (station) {
                        var msg = station.lon + '_' + station.lat;
                        if (data[station.stationId]) {
                            if (station.stationId) {
                                obj.rain1 = data[station.stationId].RAIN_24H;
                                obj.rain2 = data2[station.stationId].RAIN_6H;
								obj.rain3 = data5[station.stationId].RAIN_1H;
                            }
                        } else {
                            obj.rain1 = '';
                            obj.rain2 = '';
                        }
						var countVal = 0;
						data3[msg]['TP1H'][0].forEach(function(val){
							countVal += val;
						})
                        obj.rain3 += countVal;
                        obj.rain4 = data4[msg]['TP1H'][0][0];
                    }
                    tableData.push(obj);
				})
                for (var i = 0; i < tableData.length; i++) {
                    var $tr = $(`<tr>
                        <td><input type="text" disabled value="${tableData[i].stationName}"></td>
                        <td><input type="text" disabled value="${tableData[i].rain1 == undefined ? '' : tableData[i].rain1}"></td>
                        <td><input type="text" disabled value="${tableData[i].rain2 == undefined ? '' : tableData[i].rain2}"></td>
                        <td><input type="text" value="${tableData[i].rain3 == undefined ? '' : parseFloat(tableData[i].rain3).toFixed(1)}"></td>
                        <td><input type="text" value="${tableData[i].rain4 == undefined ? '' : tableData[i].rain4}"></td></tr>`);
                    $body.append($tr);
                }
            })
        },
        // 发布地质灾害预报
        publishDzzhybData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var dataVal = [];
            $(".dzzh_table tbody tr").each(function (i, item) {
                var list = [];
                $(item).find('td').each(function () {
                    list.push($(this).find('input').val());
                })
                dataVal.push(list)
            })
            dataVal = JSON.stringify(dataVal);
            var tempData = {
                "tempData": {
                    "curTagData": {
                        "g_dzzhyb_001": {
                            "val": dataVal
                        }
                    }
                }
            }
            tempData = JSON.stringify(tempData)
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                forecasterId: that.userInfo.smuser.iid,
                jsonStr: tempData,
				taskId: productInfo.taskId
            }
            $.post(api.internal.dzzhYb.pushforeDZZHYB, params, function (res) {
                if (res.isSuccess) {
                    alert("发布成功");
                    that.getDzzhybDataDetail();
                    CafeJS.menuFunc.setTask();
                } else {
                    alert("发布失败");
                }
            })
        },
        // 获取地质灾害预报数据
        getDzzhybDataDetail: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = new Date().format('yyyy-MM-dd 08:00:00')
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                queryTime: time
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initDzzhStation();
                } else {
                    that.buildDzzhybTable(res.data);
                }
            })
        },
        buildDzzhybTable: function (data) {
            var $body = $(".dzzh_table tbody").empty();
            var item = JSON.parse(data.data.content)
            item = JSON.parse(item.tempData.curTagData.g_dzzhyb_001.val)
            for (var i = 0; i < item.length; i++) {
                var tr = '<tr>'
                for (var j = 0; j < item[i].length; j++) {
                    if (j == 0 || j == 1 || j == 2) {
                        tr += '<td><input type="text" disabled value="' + item[i][j] + '"></td>'
                    } else {
                        tr += '<td><input type="text" value="' + item[i][j] + '"></td>'
                    }
                }
                tr += '</tr>'
                $body.append(tr);
            }
            $("#proText").val(data.txtContent);
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.publishDzzhybData();
            })
            $("#returnBtn").click(function () {
                that.initDzzhStation();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})